// pages/takePhoto/takePhoto.js
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    windowHeight:0,
    windowWidth:0,
    screenMaxBright:0,//屏幕是否最大亮度
    showCamera:false,//渲染takePhoto页
    camera:{
      mode:"normal",
      resolution:"high",
      // position:getApp().globalData.camera.direction,
      flash:"off",
      framesize:"large",
      bindstop:"exceptionalStop",
      bindinitdone:"hideLoading",
      binderror:"grantFail"
    },
    svgOutline:""// /images/取景框/outline.svg
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getsize()
    // this.setData({
    //   ['camera.position']: getApp().globalData.camera.direction
    // })
    console.log("拍照页onload")
  },
  // scrollTo() {
  //   // 1.使用wx.createSelectorQuery()查询到需要滚动到的元素位置
  //   wx.createSelectorQuery().select('.cameraContainer').boundingClientRect(res => {
  //     // 2.使用wx.getSysTemInfo()获取设备及页面高度windowHeight（px）
  //       wx.getSystemInfo({
  //           success(ress) {
  //             console.log(res)
  //             wx.pageScrollTo({
  //               // 3. 滚动的距离根据设备的页面高度进行微调（px）
  //               scrollTop: ress.windowHeight/2-res.top*1/6,
  //               duration: 50
  //             })
  //           }
  //     })
  //   }).exec()
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log('拍照页onready')
    // wx.redirectTo({
    //   url: '../takePhoto/takePhoto',
    // })
    const that=this;
    wx.showModal({
      cancelColor: '#000',
      showCancel:true,
      content:'确认开始拍摄？',
      success:res=>{
        if(res.confirm){
          that.setData({
            showCamera:true,
            ['camera.position']: getApp().globalData.camera.direction
          })

        }
        if(res.cancel){
          let that=this;
          wx.setScreenBrightness({
            value: getApp().globalData.orignalBright,
            success(){
              that.data.screenMaxBright=0
            }
          })
          wx.navigateBack({
            delta: 1,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log("拍照页onshow")
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    getApp().globalData.direction=this.data.camera.position//返回当前相机direction给全局数据
    // console.log("拍照页hide")
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // console.log("拍照页unload")
    let that=this;
    wx.setScreenBrightness({
      value: getApp().globalData.orignalBright,
      success(){
        that.data.screenMaxBright=0
      }
    })
    let app=getApp()
    app.globalData.camera.direction=that.data.camera.position//返回当前相机direction给全局对象
    console.log('当前全局对象camera.position:'+getApp().globalData.camera.direction)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //拍照
  takePhoto() {
      console.log('拍照')
      const ctx = wx.createCameraContext()
      ctx.takePhoto({
        quality: 'high',
        success: (res) => {
          var tmpPath=res.tempImagePath;
          wx.navigateTo({
            url: '/pages/takePhoto/scanning/scanning',
            success: function(res) {
              // 通过eventChannel向被打开页面传送数据
              res.eventChannel.emit('acceptDataFromOpenerPage', { imagePath: tmpPath })
            }
          })
        }
      })
  }
  
  ,
  //切换闪光灯
  switchFlash(){
    let that=this;
    if(this.data.camera.position=='back'){ //当前是后置相机
      //开关闪光灯
      if(this.data.camera.flash=='torch'){
        wx.showToast({
          title: '关闭闪光灯',
          icon:'none',
        })
        that.setData({
          ['camera.flash']:'off',
          success:res=>{
            wx.hideToast({ })
          }
        })
        console.log(that.data.camera.flash)
      }
      else{
        wx.showToast({
          title: '打开闪光灯',
          icon:'none',
        })
        that.setData({
          ['camera.flash']:'torch',
          success:res=>{
            wx.hideToast({ })
        }})
        console.log(that.data.camera.flash)
      }
    }
    else {
      //当前是前置相机
      //wx.setScreenBrightness(Object object)
      //调节屏幕亮度
      if(this.data.screenMaxBright==1){
        wx.showToast({
          title: '关闭屏幕补光',
          icon: 'none',
          duration:1000
        })
        wx.setScreenBrightness({
          value:getApp().globalData.orignalBright,
          success(){
            that.data.screenMaxBright=0
          }
        })
        console.log("屏幕补光:"+this.data.screenMaxBright)
      }
      else{
        wx.showToast({
          title: '开启屏幕补光',
          icon:'none',
          duration:1000
        })
        wx.setScreenBrightness({
          value:1,
          success(){
            that.data.screenMaxBright=1
          }
        })
        console.log("屏幕补光:"+this.data.screenMaxBright)
      }
    }
  }
  ,
  //切换前后置摄像头
  switchPosition(){
    if(this.data.camera.position=='front'){
      wx.showToast({
        title: '切换后置相机',
        icon:'none',
      })
     this.setData({
        ['camera.position']:'back',
        success:res=>{
          wx.hideToast({ })
        }
      })
      console.log(this.data.camera.position)
  }
    else{
      wx.showToast({
        title: '切换前置相机',
        icon:'none',
      })
     this.setData({
        ['camera.position']:'front',
        success:res=>{
          wx.hideToast({ })
      }})
      console.log(this.data.camera.position)
    }
    
  },
  //相机授权失败处理
  grantFail:function(){
    const pagePath=this.getCurrentPageFullPath;//返回当前页面全路径
    wx.showModal({
      title: '提示',
      content: '请打开相机授权后继续',
      success (res) {
        if (res.confirm) {
          wx.openSetting({
            success (res) {
              console.log(res.authSetting)
              res.authSetting = {
                "scope.userInfo": true,
                "scope.camera": true
              }
              wx.redirectTo({ url: '/pages/takePhoto/takePhoto'});//重载takePhoto页面
            }
          })
          // that.onReady();
        } else if (res.cancel) {
          wx.showToast({
            icon:'none',
            title:'授权失败'
          })
          wx.navigateBack({
            delta: 1,
          })
        }
      }
    })
  }
  ,
  //设备尺寸
   getsize(){
    let that=this;
    wx.getSystemInfo({
      success(res) {
        // console.log(res)
        console.log("height="+ res.windowHeight)
        console.log("width="+res.windowWidth)
        that.setData({
          windowHeight:res.windowHeight,
          windowWidth:res.windowWidth
        })
      },
    })
    // //滚动到中间
    // that.scrollTo()
  }
})